package cn.edu.nju.askme.action;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.struts2.interceptor.SessionAware;
import org.springframework.stereotype.Component;

import cn.edu.nju.askme.model.Option;
import cn.edu.nju.askme.model.Paper;
import cn.edu.nju.askme.model.Question;
import cn.edu.nju.askme.service.IOptionService;
import cn.edu.nju.askme.service.IPaperService;
import cn.edu.nju.askme.service.IQuestionService;

import com.opensymphony.xwork2.ActionSupport;

@Component
public class PaperAddAction extends ActionSupport implements SessionAware {

	// 需要暂时存储，最后提交paper的信息，所有question和所有option
	private static Paper paper;
	private static List<Question> questionList;
	private static List<Option> allOptionList;

	// 每一步有一个问题和选项列表
	private Question question;
	private List<Option> optionList;

	private Map session;
	private IPaperService paperService;
	private IQuestionService questionService;
	private IOptionService optionService;

	public Map getSession() {
		return session;
	}

	public void setSession(Map session) {
		this.session = session;
	}

	public Paper getPaper() {
		return paper;
	}

	public void setPaper(Paper paper) {
		this.paper = paper;
	}

	public Question getQuestion() {
		return question;
	}

	public void setQuestion(Question question) {
		this.question = question;
	}

	public List<Option> getOptionList() {
		return optionList;
	}

	public void setOptionList(List<Option> optionList) {
		this.optionList = optionList;
	}

	public IPaperService getPaperService() {
		return paperService;
	}

	@Resource
	public void setPaperService(IPaperService paperService) {
		this.paperService = paperService;
	}

	public IQuestionService getQuestionService() {
		return questionService;
	}

	@Resource
	public void setQuestionService(IQuestionService questionService) {
		this.questionService = questionService;
	}

	public IOptionService getOptionService() {
		return optionService;
	}

	@Resource
	public void setOptionService(IOptionService optionService) {
		this.optionService = optionService;
	}

	public String addPaperInfo() {
		System.out.println("PaperAddAction.addPaperInfo()");
		paperService.add(paper);
		return "nextStep";
	}

	public String nextStep() {
		addQuestion();
		return "nextStep";
	}

	public void addQuestion() {
		System.out.println("PaperAddAction.addQuestion()");
		for (int i =0 ; i<optionList.size();i++) {
			Option op = optionList.get(i);
			if (op!=null) {
				op.setLabel(i+"");
				op.setQuestion(question);
				allOptionList.add(op);
			}
		}
		question.setPaper(paper);
		//question.setOptions(optionList);
		questionList.add(question);
	}

	public String complete() throws Exception {
		addQuestion();
		paperService.add(paper);
		for (Question q : questionList) {
			questionService.add(q);
		}
/*		for (Option o : allOptionList) {
			optionService.add(o);
		}*/
		return SUCCESS;
	}
}
